<template>
  <a-modal
    :maskClosable="false"
    :title="title"
    centered
    :width="700"
    v-model="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
  >
    <a-form :form="form">
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="所属行业">
        <a-cascader
          popupClassName="cascader"
          :show-search="{ filter }"
          :options="industry"
          :fieldNames="{ label: 'title', value: 'key', children: 'children' }"
          placeholder="请选择对应的标准行业"
          v-decorator="['code', { rules: [{ required: true, message: '请选择行业' }] }]"
          @change="onChange"
        />
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="行业名称">
        <a-input
          placeholder="请输入名称"
          v-decorator="[
            'name',
            {
              rules: [
                { required: true, message: '请输入名称' },
                { pattern: isExistence_, message: '名称中不能包含下划线' }
              ]
            }
          ]"
        />
        <a-input v-decorator="['id']" hidden></a-input>
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="排序">
        <a-input-number
          placeholder="排序"
          :min="0"
          v-decorator="['sort', { rules: [{ required: true, message: '请输入排序' }] }]"
        />
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="备注">
        <a-input type="textarea" placeholder="请输入备注" v-decorator="['remark']" :rows="8" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script>
import { industryAdd, industryEdit } from '@/api/system/system'
import { getIndustryCategoryAllTree } from '@/api/member/memberConfig'
import pick from 'lodash.pick'
import { findParentIds } from '@/utils/treeutil'
import { isArray } from 'lodash-es'
import { isExistence_ } from '@/utils/RegExp'
export default {
  components: {},
  data() {
    return {
      isExistence_,
      visible: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      form: this.$form.createForm(this),
      title: '',
      type: '',
      industry: []
    }
  },
  beforeCreate() {},
  created() {
    this.queryIndustryCategoryAllTree()
  },
  methods: {
    onChange(value, selectedOptions) {
      if (!this.form.getFieldValue('name')) {
        this.form.setFieldsValue({ name: selectedOptions[selectedOptions.length - 1].title })
      }
    },
    queryIndustryCategoryAllTree() {
      getIndustryCategoryAllTree().then(res => {
        this.industry = res.data || []
      })
    },
    filter(inputValue, path) {
      return path.some(option => option.title.toLowerCase().indexOf(inputValue.toLowerCase()) > -1)
    },
    add(obj) {
      this.title = obj.title
      this.type = obj.type
      this.visible = true
      this.$nextTick(() => {
        this.form.resetFields()
      })
    },
    edit(obj) {
      this.title = obj.title
      this.type = obj.type
      this.visible = true

      this.$nextTick(() => {
        if (!isArray(obj.data.code)) {
          obj.data.code = findParentIds(this.industry, 'key', obj.data.code)
        }
        this.form.setFieldsValue(pick(obj.data, 'id', 'name', 'sort', 'code', 'remark'))
      })
    },

    handleSubmit(e) {
      var _this = this
      e.preventDefault()
      this.form.validateFields((err, values) => {
        if (!err) {
          var data = JSON.parse(JSON.stringify(values))
          data.code = data.code[1]
          if (_this.type === 'add') {
            return industryAdd(data)
              .then(res => {
                _this.$message.success('新增成功', 1)
                _this.visible = false
                _this.$emit('add')
              })
              .finally(() => {
                _this.confirmLoading = false
              })
          } else {
            return industryEdit(data)
              .then(res => {
                _this.$message.success('编辑成功', 1)
                _this.visible = false
                _this.$emit('upd')
              })
              .finally(() => {
                _this.confirmLoading = false
              })
          }
        }
      })
    }
  }
}
</script>
<style scoped>
.tree {
  max-height: 400px;
  overflow: auto;
}

</style>
